package 代码随想录.哈希表.赎金信;

import java.lang.annotation.ElementType;
import java.util.HashMap;
import java.util.HashSet;

/**lc：383 赎金信
 * link：https://leetcode-cn.com/problems/ransom-note/
 * @author ChenweiLin
 * @create 2021-12-16 10:43
 */
public class RansomNote {
    public static void main(String[] args) {
        String ransomNote = "aa";
        String magazine = "aab";
        System.out.println(canConstruct(ransomNote, magazine));

    }

    public static boolean canConstruct(String ransomNote, String magazine) {
        char[] charM = magazine.toCharArray();
        HashMap<Character,Integer> map = new HashMap<>();
        for (char c : charM) {
            if (map.containsKey(c))
                map.put(c, map.get(c)+1);
            else map.put(c, 1);
        }

        char[] charR = ransomNote.toCharArray();

        for (char c : charR) {
            if (map.get(c) == null){
                return false;
            }else {
                map.put(c, map.get(c)-1);
            }
        }

        return true;
    }
}
